<template>
    <div class="user">
        <basic-container>
            <avue-crud ref="crud"
                       :page="page"
                       :data="tableData"
                       :table-loading="tableLoading"
                       :option="tableOption"
                       v-model="tableForm"
                       :permission="permissionList"
                       :before-close="beforeClose"
                       @row-save="rowSave"
                       @row-update="rowUpdate"
                       @row-del="rowDelete"
                       @search-change="searchChange"
                       @selection-change="selectChange"
                       @size-change="sizeChange"
                       @current-change="pageChange"
                       @refresh-change="refresh">
                <template slot-scope="scope" slot="menu">
                    <el-button v-if="permissionList.editBtn" icon="el-icon-menu" type="text"
                               @click="editPerms(scope.row)">权限
                    </el-button>
                </template>
            </avue-crud>
            <el-dialog
                    title="权限"
                    :visible.sync="menuDialogBox"
                    append-to-body
                    width="30%">
                <div class="tree">
                    <el-tree
                            ref="menuTree"
                            :data="menuData"
                            show-checkbox
                            default-expand-all
                            :default-checked-keys="roleMenuIdList"
                            node-key="id"
                            :props="menuTreeProps">
                    </el-tree>
                </div>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="menuDialogBox = false">取 消</el-button>
                    <el-button type="primary" @click="allocateRoleMenu">确 定</el-button>
              </span>
            </el-dialog>
        </basic-container>
    </div>
</template>
<script>
    import {mapGetters} from "vuex";
    import {concatLike, treeDataTranslate} from "@/util/util";
    import {listPage, save, updateById, removeById, listMenuIdByRoleId, allocateRoleMenu} from "@/api/sys/sysRole";
    import {list as listMenu} from "@/api/sys/sysMenu";
    import {tableOption} from '@/const/sys/sysRole';

    export default {
        data() {
            return {
                tableForm: {},
                tableData: [],
                menuData: [],
                selection: [],
                page: {
                    total: 0, // 总页数
                    currentPage: 1, // 当前页数
                    pageSize: 10, // 每页显示多少条
                    desc: 'create_time',
                },
                searchForm: {},
                tableLoading: false,
                tableOption: tableOption,
                menuDialogBox: false,
                menuTreeProps: {
                    label: 'menuName',
                    value: 'id',
                    children: 'children'
                },
                roleMenuIdList: [],
                roleMenuForm: {
                    menuIdList: []
                }
            }
        },
        watch: {},
        created() {
            this.listPage();
        },
        mounted: function () {
        },
        components: {},
        computed: {
            ...mapGetters(["permission"]),
            permissionList() {
                return {
                    viewBtn: this.vaildData(this.permission['sys:role:list'], false),
                    addBtn: this.vaildData(this.permission['sys:role:save'], false),
                    editBtn: this.vaildData(this.permission['sys:role:update'], false),
                    delBtn: this.vaildData(this.permission['sys:role:delete'], false)
                };
            },
        },
        methods: {
            listPage() {
                this.tableLoading = true;
                this.searchForm = Object.assign(this.searchForm, this.page);
                Object.keys(this.searchForm).forEach((key) => {
                    if (this.validatenull(this.searchForm[key])) {
                        this.searchForm[key] = null;
                    }
                });
                listPage(concatLike(this.searchForm, ['roleName']) || {}).then(response => {
                    this.tableData = response.data.records;
                    this.page.total = +response.data.total;
                    this.tableLoading = false;
                });
            },
            rowSave(row, done, loading) {
                save(row).then(() => {
                    this.$message({
                        message: '操作成功',
                        type: 'success',
                    });
                    this.listPage();
                    done();
                }).catch(() => {
                    loading();
                });
            },
            rowUpdate(row, index, done, loading) {
                updateById(row).then(() => {
                    this.$message({
                        message: '操作成功',
                        type: 'success',
                    });
                    this.listPage();
                    done();
                }).catch(() => {
                    loading();
                });
            },
            rowDelete(row) {
                this.$confirm("确定删除" + row.roleName + "?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(() => {
                    removeById(row.id).then(() => {
                        this.$message({
                            type: 'success',
                            message: '操作成功'
                        });
                        this.listPage();
                    });
                });
            },
            beforeClose(done) {
                done();
            },
            selectChange(row) {
                this.selection = row.map(item => item.id);
            },
            pageChange(currentPage) {
                this.page.currentPage = currentPage;
                this.listPage();
            },
            sizeChange(pageSize) {
                this.page.pageSize = pageSize;
                this.listPage();
            },
            searchChange(params, done) {
                this.page.currentPage = 1;
                this.searchForm = Object.assign({}, params);
                this.listPage();
                done();
            },
            refresh(params) {
                this.searchForm = Object.assign({}, params.searchForm);
                this.listPage();
            },
            editPerms(row) {
                this.roleMenuIdList = [];
                this.roleMenuForm = Object.assign({}, row);
                listMenu().then(res => {
                    this.menuData = treeDataTranslate(res.data);
                    listMenuIdByRoleId(row.id).then(res => {
                        this.roleMenuIdList = res.data;
                    });
                });
                this.menuDialogBox = true;
            },
            allocateRoleMenu() {
                let checkedNodes = this.$refs.menuTree.getCheckedNodes(false, true);
                let menuIdList = checkedNodes.map(el => el.id);
                allocateRoleMenu({id: this.roleMenuForm.id, menuIdList: menuIdList}).then(() => {
                    this.$message({
                        message: '操作成功',
                        type: 'success',
                    });
                    this.menuDialogBox = false;
                }).catch(() => {
                });
            }
        }
    }
</script>
<style>
    .el-tree {
        min-width: 100%;
        font-size: 14px;
        display: inline-block;;
    }

    .tree {
        overflow-y: auto;
        overflow-x: scroll;
        height: 360px;
        /*width:200px;*/
        /*border: 1px solid blue;*/
    }
</style>
